package interview.javabase.thread.aqs.countdownlatch;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class CountDownLatchDemo {

    @Test
    public void test() throws InterruptedException {
        CountDownLatch cdl = new CountDownLatch(5);
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                System.out.println("A");
                cdl.countDown();
            }).start();
            new Thread(() -> {
                System.out.println("B");
                cdl.countDown();
            }).start();
            new Thread(() -> {
                System.out.println("C");
                cdl.countDown();
            }).start();
        }
        cdl.await();
        System.out.println("com.study.normal.done");
    }

    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();

        CountDownLatch latch = new CountDownLatch(3);
        Worker w1 = new Worker(latch, "张三");
        Worker w2 = new Worker(latch, "李四");
        Worker w3 = new Worker(latch, "王二");

        Boss boss = new Boss(latch);
        executor.execute(w3);
        executor.execute(w2);
        executor.execute(w1);
        executor.execute(boss);

        executor.shutdown();
    }
}  